package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.fs.api.FsApi;
import com.sdy.supermarket.biz.model.GsCategoryManage;
import com.sdy.supermarket.biz.service.GsCooperationUnitService;
import com.sdy.supermarket.biz.model.GsCooperationUnit;
import com.sdy.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2019-08-05
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/gsCooperationUnit")
public class GsCooperationUnitController extends BaseController {
    @Autowired
    private GsCooperationUnitService gsCooperationUnitService;
    @Autowired
    private FsApi fsApi;

    /**
     * 分页查询
     *
     * @param page    size current
     * @param cooName
     * @return
     */
    @GetMapping("/pageList")
    public Response pageList(Page<GsCooperationUnit> page, String cooName) throws UnsupportedEncodingException {
        if (null != cooName) {
            cooName = URLDecoder.decode(cooName, "UTF-8");
        }
        IPage<GsCooperationUnit> iPage = gsCooperationUnitService.pageList(page, cooName);
        return Response.success(iPage);
    }

    /**
     * 根据ID删除数据
     *
     * @param gsCategoryManage
     * @return
     */
    @PostMapping("/delById")
    public Response deleteById(@RequestBody GsCategoryManage gsCategoryManage) throws BizException {
        int i = gsCooperationUnitService.deleteById(gsCategoryManage.getId());
        return i != 0 ? Response.success("操作成功") : Response.error("操作失败");
    }

    /**
     * 排序上升&降低
     *
     * @param map id type
     * @return
     */
    @PostMapping("/sortUpOrDown")
    public Response sortUpOrDown(@RequestBody HashMap<String, Object> map) throws BizException {
        int i = gsCooperationUnitService.sortUpOrDown((Integer) map.get("id"), (Integer) map.get("type"));
        return i != 0 ? Response.success("修改成功") : Response.error("修改失败");
    }

    /**
     * 新增&修改方法
     *
     * @param cooperationUnit
     * @return
     * @throws BizException
     * @throws IOException
     */
    @PostMapping("/saveOrUpdate")
    public Response saveOrUpdateCoo(@RequestBody GsCooperationUnit cooperationUnit) throws BizException {
        int i = gsCooperationUnitService.saveOrUpdateCoo(cooperationUnit);
        return i != 0 ? Response.success("操作成功") : Response.error("操作失败");
    }

    /**
     * 获取单条数据
     *
     * @param id
     * @return
     * @throws BizException
     */
    @GetMapping("/getById")
    public Response getById(Integer id) throws BizException {
        Assert.isNull(id, "ID为NULL");
        GsCooperationUnit byId = gsCooperationUnitService.getById(id);
        if (StringUtil.isNotBlank(byId.getLogoImgUrl()) && StringUtil.isNotEmpty(byId.getLogoImgUrl())) {
            byId.setFileInfo(fsApi.getFileInfo(byId.getLogoImgUrl()));
        } else {
            byId.setFileInfo(null);
        }
        return Response.success(byId);
    }

}
